package com.example.studentsRoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentsRoom.entity.Building;
import com.example.studentsRoom.entity.Student;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.mapper.BuildingMapper;
import com.example.studentsRoom.mapper.DormitoryAdminMapper;
import com.example.studentsRoom.service.IBuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentsRoom.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@Service
public class BuildingService extends ServiceImpl<BuildingMapper, Building> implements IBuildingService {

    @Autowired
    BuildingMapper buildingMapper;

    @Autowired
    DormitoryAdminMapper dormitoryAdminMapper;

    /**
     * 分压查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageVo listPage(Integer page, Integer size) {
        Page<Building> buildingPage = new Page<>(page, size);

        Page<Building> resultPage = this.buildingMapper.selectPage(buildingPage, null);

        PageVo pageVo = BuildAndDormitoryVo.transition(resultPage, dormitoryAdminMapper);
        return pageVo;

    }

    /**
     * 模糊查询+分页
     *
     * @param searchForm
     * @return
     */
    @Override
    public PageVo search(SearchForm searchForm) {
        Page<Building> buildingPag = new Page<>(searchForm.getPage(), searchForm.getSize());

        //模糊查询加分页
        Page<Building> resultPage = null;

        //判断value值是不是null
        if (searchForm.getValue().equals("")) {
            resultPage = this.buildingMapper.selectPage(buildingPag, null);
        } else {
            QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.buildingMapper.selectPage(buildingPag, queryWrapper);
        }

        PageVo pageVo = BuildAndDormitoryVo.transition(resultPage, dormitoryAdminMapper);
        return pageVo;
    }

    /**
     * 根据宿舍，模糊查询楼宇
     * ------->dormitory
     * @param searchForm
     * @return
     */
    @Override
    public SearchForm getBid(SearchForm searchForm) {
        QueryWrapper<Building> wrapper=new QueryWrapper<>();
        wrapper.like("b_msg",searchForm.getValue());

        //只要有宿舍楼，那么bid就只有一个，所以保存为一个对象就行
        Building building = buildingMapper.selectOne(wrapper);
        SearchForm form=new SearchForm();
        form.setValue(String.valueOf(building.getBId()));

        return form;
    }


}
